package com.zf.zfed.audit.set

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.blankj.utilcode.util.FragmentUtils
import com.zf.zfed.audit.tools.ActivityCollector

/**
 * @Author: dw
 * @Date: 2024/07/23 19:23
 * @Description:
 */
object Navigation {
    private var fragmentManager: FragmentManager? = null

    private var containerId: Int? = null

    fun setFragmentManager(fragmentManager: FragmentManager, containerId: Int) {
        this.fragmentManager = fragmentManager
        this.containerId = containerId
    }

    // 压栈
    fun push(fragment: Fragment) {
        try {
            fragmentManager?.let { fm ->
                containerId?.let { cId ->
                    FragmentUtils.add(
                        fm,
                        fragment,
                        cId,
                        fragment.toString(),
                        false,
                        true
                    )
                    // 隐藏Tab
                    ActivityCollector.rootActivity().hideBottomTabBar()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    // 出栈
    fun pop() {
        try {
            fragmentManager?.let {
                if (FragmentUtils.getAllFragmentsInStack(it).count() == 1) {
                    ActivityCollector.rootActivity().showBottomTabBar()
                    fragmentManager = null
                    containerId = null
                }
                FragmentUtils.pop(it, true)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }


    }

    fun clear() {
        try {
            fragmentManager?.let {
                FragmentUtils.removeAll(it)
            }
            fragmentManager = null
            containerId = null
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}